#pragma once

class CLayoutGroup;

class CLayoutGroupDependencyAnalyzer
{
public:
	~CLayoutGroupDependencyAnalyzer();

	// create optimal order of groups in the exported file, returns false if no such order exist
	bool CreateOptimalGroupOrder( const CLayoutDefinition& layout, std::vector< const CLayoutGroup* >& outGroups );

private:
	struct GroupDependencies
	{
		const CLayoutGroup*						m_group;
		std::vector< GroupDependencies* >		m_dependencies;
		int										m_order;

		GroupDependencies( const CLayoutGroup* group )
			: m_group( group )
			, m_order( 0 )
		{}

		void AddDependency( GroupDependencies* otherGroup )
		{
			if ( m_dependencies.end() == std::find( m_dependencies.begin(), m_dependencies.end(), otherGroup ) )
				m_dependencies.push_back( otherGroup );
		}	

		static inline bool SortByDepth( const GroupDependencies* a, const GroupDependencies* b )
		{
			return a->m_order > b->m_order;
		}
	};

	std::vector< GroupDependencies* >		m_groups;

	// create wrapper for given group
	GroupDependencies* GetWrapper( const CLayoutGroup* group );

	// create wrapper and export dependencies
	void AddGroup( const CLayoutGroup* group );
};